import { defineStore } from 'pinia';
import {
  LoginData,
} from '@/api/user';
import { setTokenU, clearTokenU } from '@/utils/auth';
// import {
//   loginByUserNameVerify, 
//   getUserInfobyUsername, 
//   userLogout
// } from '@/api/auth.js';
import { removeRouteListener } from '@/utils/route-listener';
import { UserState } from './types';
import useAppStore from '../app';

const useUserStore = defineStore('user', {
  state: (): UserState => ({
    name: undefined,
    avatar: undefined,
    job: undefined,
    organization: undefined,
    location: undefined,
    email: undefined,
    introduction: undefined,
    personalWebsite: undefined,
    jobName: undefined,
    organizationName: undefined,
    locationName: undefined,
    phone: undefined,
    registrationDate: undefined,
    accountId: undefined,
    certification: undefined,
    role: '',
  }),

  getters: {
    userInfo(state: UserState): UserState {
      return { ...state };
    },
  },

  actions: {
  //   switchRoles() {
  //     return new Promise((resolve) => {
  //       this.role = this.role === 'user' ? 'admin' : 'user';
  //       resolve(this.role);
  //     });
  //   },
  //   // Set user's information
  //   setInfo(partial: Partial<UserState>) {
  //     this.$patch(partial);
  //   },

    // Reset user's information
    resetInfo() {
      this.$reset();
    },

  //   // Get user's information
  //   async info() {
  //     const res = await getUserInfobyUsername();
  //     res.data.data.role = 'admin'
  //     this.setInfo(res.data.data);
  //   },

    // Login
    async login() {
      try {
        //const res = await userLogin(loginForm);
        // const res = await loginByUserNameVerify(loginForm)
        console.log(1);
        setTokenU("Dealth_Knight")
      } catch (err) {
        clearTokenU();
        throw err;
      }
    },
    logoutCallBack() {
      const appStore = useAppStore();
      this.resetInfo();
      clearTokenU();
      removeRouteListener();
      appStore.clearServerMenu();
    },
    // Logout
    async logout() {
      try {
        // await userLogout();
      } finally {
        this.logoutCallBack();
      }
    },
  },
});

export default useUserStore;
